﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Data.SqlClient;
using System.Data; 

namespace StartStop.Helpers
{
    public class EventLog
    {
        public const string LiveConnectionString = "data source=v2.startstop.me;initial catalog=startstop;persist security info=True;user id=sa;password=PASSWORD;";

        public static void LogError (ServiceResources.ErrorEvent oEvent)
        {

            ServiceResources.MessageResponse oResponse = new ServiceResources.MessageResponse();
            SqlConnection oConn = new SqlConnection(LiveConnectionString);
            oConn.Open();

            SqlCommand oCommand = new SqlCommand(@" 

                                                    insert into EventLogs (EventFrom, ErrorMessage, ErrorDetail, DBObject, DBError, Method, ErrorLevel) 
                                                    values (@EventFrom, @ErrorMessage, @ErrorDetail, @DBObject, @DBError, @Method, @ErrorLevel); select @@IDENTITY;


", oConn);
            if (oEvent.ErrorFrom.ToString() != "")
            {
                oCommand.Parameters.AddWithValue("@EventFrom", oEvent.ErrorFrom);
            }
            else
            {
                oCommand.Parameters.AddWithValue("@EventFrom", System.DBNull.Value);
            }

            if (oEvent.ErrorMessage != null)
            {
                oCommand.Parameters.AddWithValue("@ErrorMessage", oEvent.ErrorMessage);
            }
            else
            {
                oCommand.Parameters.AddWithValue("@ErrorMessage", System.DBNull.Value); 
            }

            if (oEvent.ErrorDetail != null)
            {
                oCommand.Parameters.AddWithValue("@ErrorDetail", oEvent.ErrorDetail);
            }
            else
            {
                oCommand.Parameters.AddWithValue("@ErrorDetail", System.DBNull.Value);
            }

            if (oEvent.DBObject != null)
            {
                oCommand.Parameters.AddWithValue("@DBObject", oEvent.DBObject);
            }
            else
            {
                oCommand.Parameters.AddWithValue("@DBObject", System.DBNull.Value);
            }

            if (oEvent.Method != null)
            {
                oCommand.Parameters.AddWithValue("@Method", oEvent.Method);
            }
            else
            {
                oCommand.Parameters.AddWithValue("@Method", System.DBNull.Value);
            }

            if (oEvent.DBError != null)
            {
                oCommand.Parameters.AddWithValue("@DBError", oEvent.DBError);
            }
            else
            {
                oCommand.Parameters.AddWithValue("@DBError", System.DBNull.Value); 
            }

            oCommand.Parameters.AddWithValue("@ErrorLevel", oEvent.ErrorLevel);

            oCommand.ExecuteNonQuery(); 

            oConn.Close(); 
        }

    }
}
